﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BaHuan.Common;
using BaHuan.Entities;
using System.Data.SqlClient;
using System.Data;

namespace BaHuan.DataLayer
{
    public class BangMaPhieuDAO:BaseDAO
    {
        // save into DB, @id = -1: insert else, update
        public ResultObj SaveBangMaPhieu(BangMaPhieu bangmaphieu)
        {
            ResultObj result = new ResultObj();
            result.Result = null;
            SqlConnection conObj = DataConnection.GetSqlConnection();
            SqlCommand cmd = CreateSqlCommand("spAddBangMaPhieu", CommandType.StoredProcedure);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "spAddBangMaPhieu";
            SqlParameter param = new SqlParameter();
            param = CreateParameter("@Id", DbType.Int32, ParameterDirection.Input, bangmaphieu.Id);
            cmd.Parameters.Add(param);
            param = CreateParameter("@MaSo", DbType.String, ParameterDirection.Input, bangmaphieu.MaSo);
            cmd.Parameters.Add(param);
            param = CreateParameter("@IdLoaiPhieu", DbType.Int32, ParameterDirection.Input, bangmaphieu.IdLoaiPhieu);
            cmd.Parameters.Add(param);
            param = CreateParameter("@DienGiai", DbType.String, ParameterDirection.Input, bangmaphieu.DienGiai);
            cmd.Parameters.Add(param);
            param = CreateParameter("@ReturnVal", DbType.String, ParameterDirection.ReturnValue, result.ReturnCode);
            cmd.Parameters.Add(param);

            conObj.Open();
            cmd.ExecuteNonQuery();
            conObj.Close();

            result.ReturnCode = Convert.ToInt32(cmd.Parameters["@ReturnVal"].Value);
            return result;
        }

        // Get all BangMaPhieu in DB
        public ResultObj GetAllBangMaPhieu()
        {
            ResultObj result = new ResultObj();
            result.Result = null;
            List<BangMaPhieu> lst = null;
            SqlCommand cmd = CreateSqlCommand("spGetAllBangMaPhieu", CommandType.StoredProcedure);
            cmd.Connection.Open();
            using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
            {
                lst = new List<BangMaPhieu>();
                BangMaPhieu item = null;
                // reader all data from DB one by one
                while (reader.Read())
                {
                    item = new BangMaPhieu();
                    item.Id = Convert.ToInt32(reader["Id"]);
                    item.MaSo = Convert.ToString(reader["MaSo"]);
                    item.IdLoaiPhieu = Convert.ToInt32(reader["IdLoaiPhieu"]);
                    item.DienGiai = Convert.ToString(reader["DienGiai"]);
                    lst.Add(item);
                }
            }
            result.Result = lst;
            result.ReturnCode = 1;

            return result;
        }

        // Get all BangMaPhieu in DB with condition
        public ResultObj GetAllBangMaPhieuByIDLoaiPhieu(int id)
        {
            ResultObj result = new ResultObj();
            result.Result = null;
            List<BangMaPhieu> lst = null;
            SqlCommand cmd = CreateSqlCommand("spGetAllBangMaPhieuByIDLoaiPhieu", CommandType.StoredProcedure);
            SqlParameter param = CreateParameter("@IdLoaiPhieu", DbType.Int32, ParameterDirection.Input, id);
            cmd.Parameters.Add(param);
            cmd.Connection.Open();
            using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
            {
                lst = new List<BangMaPhieu>();
                BangMaPhieu item = null;
                // reader all data from DB one by one
                while (reader.Read())
                {
                    item = new BangMaPhieu();
                    item.Id = Convert.ToInt32(reader["Id"]);
                    item.MaSo = Convert.ToString(reader["MaSo"]);
                    item.IdLoaiPhieu = Convert.ToInt32(reader["IdLoaiPhieu"]);
                    item.DienGiai = Convert.ToString(reader["DienGiai"]);
                    lst.Add(item);
                }
            }
            result.Result = lst;
            result.ReturnCode = 1;

            return result;
        }

        // Delete a BangMaPhieu out of DB with condition
        public ResultObj DeleteBangMaPhieu(BangMaPhieu bangmaphieu)
        {
            ResultObj result = new ResultObj();
            result.Result = null;
            SqlConnection conObj = DataConnection.GetSqlConnection();
            SqlCommand cmd = CreateSqlCommand("spDeleteBangMaPhieu", CommandType.StoredProcedure);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "spDeleteBangMaPhieu";
            SqlParameter param = CreateParameter("@Id", DbType.Int32, ParameterDirection.Input, bangmaphieu.Id);
            cmd.Parameters.Add(param);
            conObj.Open();
            result.ReturnCode = cmd.ExecuteNonQuery();
            conObj.Close();

            return result;
        }
    }
}
